﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using QLHD.BLL;
using QLHD.DAL;

namespace QLHD
{
    public partial class frmNhaCC : Form
    {
        public frmNhaCC() { }
        public frmNhaCC(frmMain form)
        {
            InitializeComponent();
            MdiParent = form;
        }

        private void frmNhaCC_Load(object sender, EventArgs e)
        {
            ReadOnlyTextbox();
            dgvNCC.DataSource = NhaCC_DB.selectNCC();
            txtMaNCC.Text = dgvNCC[0, 0].Value.ToString();
            txtTenNCC.Text = dgvNCC[1, 0].Value.ToString();
            txtDiaChi.Text = dgvNCC[2, 0].Value.ToString();
            txtSDT.Text = dgvNCC[3, 0].Value.ToString();
        }

        private void dgvNCC_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txtMaNCC.Text = dgvNCC.Rows[e.RowIndex].Cells[0].Value.ToString();
            txtTenNCC.Text = dgvNCC.Rows[e.RowIndex].Cells[1].Value.ToString();
            txtDiaChi.Text = dgvNCC.Rows[e.RowIndex].Cells[2].Value.ToString();
            txtSDT.Text = dgvNCC.Rows[e.RowIndex].Cells[3].Value.ToString();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (btnThem.Text == "Thêm")
            {
                btnThem.Text = "Lưu";
                NotReadOnlyTextbox();
                ClearTextbox();
            }
            else
            {
                InsertNCC();
                frmNhaCC_Load(sender, e);
                btnThem.Text = "Thêm";
            }
            
            
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            NhaCC NCC = new NhaCC();
            NCC.MANCC = txtMaNCC.Text;

            NhaCC_DB.DeleteNCC(NCC);
            frmNhaCC_Load(sender, e);
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (btnSua.Text == "Sửa")
            {
                btnSua.Text = "Lưu";
                NotReadOnlyTextbox();
                txtMaNCC.ReadOnly = true;
            }
            else
            {                
                UpdateNCC();
                frmNhaCC_Load(sender, e);
                btnSua.Text = "Sửa";
                
            }
            
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            frmNhaCC_Load(sender, e);
            btnThem.Text = "Thêm";
            btnSua.Text = "Sửa";
        }
        void ReadOnlyTextbox()
        {
            txtMaNCC.ReadOnly = true;
            txtTenNCC.ReadOnly = true;
            txtDiaChi.ReadOnly = true;
            txtSDT.ReadOnly = true;
        }
        void NotReadOnlyTextbox()
        {
            txtMaNCC.ReadOnly = false;
            txtTenNCC.ReadOnly = false;
            txtDiaChi.ReadOnly = false;
            txtSDT.ReadOnly = false;
        }
        void InsertNCC()
        {
            try
            {
                NhaCC NCC = new NhaCC();
                NCC.MANCC = txtMaNCC.Text;
                NCC.TENNCC = txtTenNCC.Text;
                NCC.DIACHI = txtDiaChi.Text;
                NCC.DIENTHOAI = txtSDT.Text;

                NhaCC_DB.InsertNCC(NCC);                
            }
            catch (SqlException ex)
            {
                string message = "Xảy ra lỗi:" + "\n" + ex.Message;
                MessageBox.Show(message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            
            
        }
        void ClearTextbox()
        {
            txtMaNCC.Clear();
            txtTenNCC.Clear();
            txtDiaChi.Clear();
            txtSDT.Clear();
        }
        void UpdateNCC()
        {
            try
            {
                NhaCC NCC = new NhaCC();
                NCC.MANCC = txtMaNCC.Text;
                NCC.TENNCC = txtTenNCC.Text;
                NCC.DIACHI = txtDiaChi.Text;
                NCC.DIENTHOAI = txtSDT.Text;
                NhaCC_DB.UpdateNCC(NCC);
            }
            catch (SqlException ex)
            {
                string message = "Xảy ra lỗi:" + "\n" + ex.Message;
                MessageBox.Show(message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);                
            }
  

            
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
